<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>tablesorter.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_ui_tablesorter.js.html">tablesorter.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview A table sorting decorator.
<a name="line17"></a> *
<a name="line18"></a> * @author robbyw@google.com (Robby Walker)
<a name="line19"></a> * @see ../demos/tablesorter.html
<a name="line20"></a> */
<a name="line21"></a>
<a name="line22"></a>goog.provide(&#39;goog.ui.TableSorter&#39;);
<a name="line23"></a>goog.provide(&#39;goog.ui.TableSorter.EventType&#39;);
<a name="line24"></a>
<a name="line25"></a>goog.require(&#39;goog.array&#39;);
<a name="line26"></a>goog.require(&#39;goog.dom&#39;);
<a name="line27"></a>goog.require(&#39;goog.dom.TagName&#39;);
<a name="line28"></a>goog.require(&#39;goog.dom.classlist&#39;);
<a name="line29"></a>goog.require(&#39;goog.events.EventType&#39;);
<a name="line30"></a>goog.require(&#39;goog.functions&#39;);
<a name="line31"></a>goog.require(&#39;goog.ui.Component&#39;);
<a name="line32"></a>
<a name="line33"></a>
<a name="line34"></a>
<a name="line35"></a>/**
<a name="line36"></a> * A table sorter allows for sorting of a table by column.  This component can
<a name="line37"></a> * be used to decorate an already existing TABLE element with sorting
<a name="line38"></a> * features.
<a name="line39"></a> *
<a name="line40"></a> * The TABLE should use a THEAD containing TH elements for the table column
<a name="line41"></a> * headers.
<a name="line42"></a> *
<a name="line43"></a> * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper, used for
<a name="line44"></a> *     document interaction.
<a name="line45"></a> * @constructor
<a name="line46"></a> * @extends {goog.ui.Component}
<a name="line47"></a> */
<a name="line48"></a>goog.ui.TableSorter = function(opt_domHelper) {
<a name="line49"></a>  goog.ui.Component.call(this, opt_domHelper);
<a name="line50"></a>
<a name="line51"></a>  /**
<a name="line52"></a>   * The current sort header of the table, or null if none.
<a name="line53"></a>   * @type {HTMLTableCellElement}
<a name="line54"></a>   * @private
<a name="line55"></a>   */
<a name="line56"></a>  this.header_ = null;
<a name="line57"></a>
<a name="line58"></a>  /**
<a name="line59"></a>   * Whether the last sort was in reverse.
<a name="line60"></a>   * @type {boolean}
<a name="line61"></a>   * @private
<a name="line62"></a>   */
<a name="line63"></a>  this.reversed_ = false;
<a name="line64"></a>
<a name="line65"></a>  /**
<a name="line66"></a>   * The default sorting function.
<a name="line67"></a>   * @type {function(*, *) : number}
<a name="line68"></a>   * @private
<a name="line69"></a>   */
<a name="line70"></a>  this.defaultSortFunction_ = goog.ui.TableSorter.numericSort;
<a name="line71"></a>
<a name="line72"></a>  /**
<a name="line73"></a>   * Array of custom sorting functions per colun.
<a name="line74"></a>   * @type {Array.&lt;function(*, *) : number&gt;}
<a name="line75"></a>   * @private
<a name="line76"></a>   */
<a name="line77"></a>  this.sortFunctions_ = [];
<a name="line78"></a>};
<a name="line79"></a>goog.inherits(goog.ui.TableSorter, goog.ui.Component);
<a name="line80"></a>
<a name="line81"></a>
<a name="line82"></a>/**
<a name="line83"></a> * Row number (in &lt;thead&gt;) to use for sorting.
<a name="line84"></a> * @type {number}
<a name="line85"></a> * @private
<a name="line86"></a> */
<a name="line87"></a>goog.ui.TableSorter.prototype.sortableHeaderRowIndex_ = 0;
<a name="line88"></a>
<a name="line89"></a>
<a name="line90"></a>/**
<a name="line91"></a> * Sets the row index (in &lt;thead&gt;) to be used for sorting.
<a name="line92"></a> * By default, the first row (index 0) is used.
<a name="line93"></a> * Must be called before decorate() is called.
<a name="line94"></a> * @param {number} index The row index.
<a name="line95"></a> */
<a name="line96"></a>goog.ui.TableSorter.prototype.setSortableHeaderRowIndex = function(index) {
<a name="line97"></a>  if (this.isInDocument()) {
<a name="line98"></a>    throw Error(goog.ui.Component.Error.ALREADY_RENDERED);
<a name="line99"></a>  }
<a name="line100"></a>  this.sortableHeaderRowIndex_ = index;
<a name="line101"></a>};
<a name="line102"></a>
<a name="line103"></a>
<a name="line104"></a>/**
<a name="line105"></a> * Table sorter events.
<a name="line106"></a> * @enum {string}
<a name="line107"></a> */
<a name="line108"></a>goog.ui.TableSorter.EventType = {
<a name="line109"></a>  BEFORESORT: &#39;beforesort&#39;,
<a name="line110"></a>  SORT: &#39;sort&#39;
<a name="line111"></a>};
<a name="line112"></a>
<a name="line113"></a>
<a name="line114"></a>/** @override */
<a name="line115"></a>goog.ui.TableSorter.prototype.canDecorate = function(element) {
<a name="line116"></a>  return element.tagName == goog.dom.TagName.TABLE;
<a name="line117"></a>};
<a name="line118"></a>
<a name="line119"></a>
<a name="line120"></a>/** @override */
<a name="line121"></a>goog.ui.TableSorter.prototype.enterDocument = function() {
<a name="line122"></a>  goog.ui.TableSorter.superClass_.enterDocument.call(this);
<a name="line123"></a>
<a name="line124"></a>  var table = this.getElement();
<a name="line125"></a>  var headerRow = table.tHead.rows[this.sortableHeaderRowIndex_];
<a name="line126"></a>
<a name="line127"></a>  this.getHandler().listen(headerRow, goog.events.EventType.CLICK, this.sort_);
<a name="line128"></a>};
<a name="line129"></a>
<a name="line130"></a>
<a name="line131"></a>/**
<a name="line132"></a> * @return {number} The current sort column of the table, or -1 if none.
<a name="line133"></a> */
<a name="line134"></a>goog.ui.TableSorter.prototype.getSortColumn = function() {
<a name="line135"></a>  return this.header_ ? this.header_.cellIndex : -1;
<a name="line136"></a>};
<a name="line137"></a>
<a name="line138"></a>
<a name="line139"></a>/**
<a name="line140"></a> * @return {boolean} Whether the last sort was in reverse.
<a name="line141"></a> */
<a name="line142"></a>goog.ui.TableSorter.prototype.isSortReversed = function() {
<a name="line143"></a>  return this.reversed_;
<a name="line144"></a>};
<a name="line145"></a>
<a name="line146"></a>
<a name="line147"></a>/**
<a name="line148"></a> * @return {function(*, *) : number} The default sort function to be used by
<a name="line149"></a> *     all columns.
<a name="line150"></a> */
<a name="line151"></a>goog.ui.TableSorter.prototype.getDefaultSortFunction = function() {
<a name="line152"></a>  return this.defaultSortFunction_;
<a name="line153"></a>};
<a name="line154"></a>
<a name="line155"></a>
<a name="line156"></a>/**
<a name="line157"></a> * Sets the default sort function to be used by all columns.  If not set
<a name="line158"></a> * explicitly, this defaults to numeric sorting.
<a name="line159"></a> * @param {function(*, *) : number} sortFunction The new default sort function.
<a name="line160"></a> */
<a name="line161"></a>goog.ui.TableSorter.prototype.setDefaultSortFunction = function(sortFunction) {
<a name="line162"></a>  this.defaultSortFunction_ = sortFunction;
<a name="line163"></a>};
<a name="line164"></a>
<a name="line165"></a>
<a name="line166"></a>/**
<a name="line167"></a> * Gets the sort function to be used by the given column.  Returns the default
<a name="line168"></a> * sort function if no sort function is explicitly set for this column.
<a name="line169"></a> * @param {number} column The column index.
<a name="line170"></a> * @return {function(*, *) : number} The sort function used by the column.
<a name="line171"></a> */
<a name="line172"></a>goog.ui.TableSorter.prototype.getSortFunction = function(column) {
<a name="line173"></a>  return this.sortFunctions_[column] || this.defaultSortFunction_;
<a name="line174"></a>};
<a name="line175"></a>
<a name="line176"></a>
<a name="line177"></a>/**
<a name="line178"></a> * Set the sort function for the given column, overriding the default sort
<a name="line179"></a> * function.
<a name="line180"></a> * @param {number} column The column index.
<a name="line181"></a> * @param {function(*, *) : number} sortFunction The new sort function.
<a name="line182"></a> */
<a name="line183"></a>goog.ui.TableSorter.prototype.setSortFunction = function(column, sortFunction) {
<a name="line184"></a>  this.sortFunctions_[column] = sortFunction;
<a name="line185"></a>};
<a name="line186"></a>
<a name="line187"></a>
<a name="line188"></a>/**
<a name="line189"></a> * Sort the table contents by the values in the given column.
<a name="line190"></a> * @param {goog.events.BrowserEvent} e The click event.
<a name="line191"></a> * @private
<a name="line192"></a> */
<a name="line193"></a>goog.ui.TableSorter.prototype.sort_ = function(e) {
<a name="line194"></a>  // Determine what column was clicked.
<a name="line195"></a>  // TODO(robbyw): If this table cell contains another table, this could break.
<a name="line196"></a>  var target = /** @type {Node} */ (e.target);
<a name="line197"></a>  var th = goog.dom.getAncestorByTagNameAndClass(target,
<a name="line198"></a>      goog.dom.TagName.TH);
<a name="line199"></a>
<a name="line200"></a>  // If the user clicks on the same column, sort it in reverse of what it is
<a name="line201"></a>  // now.  Otherwise, sort forward.
<a name="line202"></a>  var reverse = th == this.header_ ? !this.reversed_ : false;
<a name="line203"></a>
<a name="line204"></a>  // Perform the sort.
<a name="line205"></a>  if (this.dispatchEvent(goog.ui.TableSorter.EventType.BEFORESORT)) {
<a name="line206"></a>    if (this.sort(th.cellIndex, reverse)) {
<a name="line207"></a>      this.dispatchEvent(goog.ui.TableSorter.EventType.SORT);
<a name="line208"></a>    }
<a name="line209"></a>  }
<a name="line210"></a>};
<a name="line211"></a>
<a name="line212"></a>
<a name="line213"></a>/**
<a name="line214"></a> * Sort the table contents by the values in the given column.
<a name="line215"></a> * @param {number} column The column to sort by.
<a name="line216"></a> * @param {boolean=} opt_reverse Whether to sort in reverse.
<a name="line217"></a> * @return {boolean} Whether the sort was executed.
<a name="line218"></a> */
<a name="line219"></a>goog.ui.TableSorter.prototype.sort = function(column, opt_reverse) {
<a name="line220"></a>  var sortFunction = this.getSortFunction(column);
<a name="line221"></a>  if (sortFunction === goog.ui.TableSorter.noSort) {
<a name="line222"></a>    return false;
<a name="line223"></a>  }
<a name="line224"></a>
<a name="line225"></a>  // Remove old header classes.
<a name="line226"></a>  if (this.header_) {
<a name="line227"></a>    goog.dom.classlist.remove(this.header_, this.reversed_ ?
<a name="line228"></a>        goog.getCssName(&#39;goog-tablesorter-sorted-reverse&#39;) :
<a name="line229"></a>        goog.getCssName(&#39;goog-tablesorter-sorted&#39;));
<a name="line230"></a>  }
<a name="line231"></a>
<a name="line232"></a>  // If the user clicks on the same column, sort it in reverse of what it is
<a name="line233"></a>  // now.  Otherwise, sort forward.
<a name="line234"></a>  this.reversed_ = !!opt_reverse;
<a name="line235"></a>  var multiplier = this.reversed_ ? -1 : 1;
<a name="line236"></a>  var cmpFn = function(a, b) {
<a name="line237"></a>    return multiplier * sortFunction(a[0], b[0]) || a[1] - b[1];
<a name="line238"></a>  };
<a name="line239"></a>
<a name="line240"></a>  // Sort all tBodies
<a name="line241"></a>  var table = this.getElement();
<a name="line242"></a>  goog.array.forEach(table.tBodies, function(tBody) {
<a name="line243"></a>    // Collect all of the rows into an array.
<a name="line244"></a>    var values = goog.array.map(tBody.rows, function(row, rowIndex) {
<a name="line245"></a>      return [goog.dom.getTextContent(row.cells[column]), rowIndex, row];
<a name="line246"></a>    });
<a name="line247"></a>
<a name="line248"></a>    goog.array.sort(values, cmpFn);
<a name="line249"></a>
<a name="line250"></a>    // Remove the tBody temporarily since this speeds up the sort on some
<a name="line251"></a>    // browsers.
<a name="line252"></a>    var nextSibling = tBody.nextSibling;
<a name="line253"></a>    table.removeChild(tBody);
<a name="line254"></a>
<a name="line255"></a>    // Sort the rows, using the resulting array.
<a name="line256"></a>    goog.array.forEach(values, function(row) {
<a name="line257"></a>      tBody.appendChild(row[2]);
<a name="line258"></a>    });
<a name="line259"></a>
<a name="line260"></a>    // Reinstate the tBody.
<a name="line261"></a>    table.insertBefore(tBody, nextSibling);
<a name="line262"></a>  });
<a name="line263"></a>
<a name="line264"></a>  // Mark this as the last sorted column.
<a name="line265"></a>  this.header_ = table.tHead.rows[this.sortableHeaderRowIndex_].cells[column];
<a name="line266"></a>
<a name="line267"></a>  // Update the header class.
<a name="line268"></a>  goog.dom.classlist.add(this.header_, this.reversed_ ?
<a name="line269"></a>      goog.getCssName(&#39;goog-tablesorter-sorted-reverse&#39;) :
<a name="line270"></a>      goog.getCssName(&#39;goog-tablesorter-sorted&#39;));
<a name="line271"></a>
<a name="line272"></a>  return true;
<a name="line273"></a>};
<a name="line274"></a>
<a name="line275"></a>
<a name="line276"></a>/**
<a name="line277"></a> * Disables sorting on the specified column
<a name="line278"></a> * @param {*} a First sort value.
<a name="line279"></a> * @param {*} b Second sort value.
<a name="line280"></a> * @return {number} Negative if a &lt; b, 0 if a = b, and positive if a &gt; b.
<a name="line281"></a> */
<a name="line282"></a>goog.ui.TableSorter.noSort = goog.functions.error(&#39;no sort&#39;);
<a name="line283"></a>
<a name="line284"></a>
<a name="line285"></a>/**
<a name="line286"></a> * A numeric sort function.
<a name="line287"></a> * @param {*} a First sort value.
<a name="line288"></a> * @param {*} b Second sort value.
<a name="line289"></a> * @return {number} Negative if a &lt; b, 0 if a = b, and positive if a &gt; b.
<a name="line290"></a> */
<a name="line291"></a>goog.ui.TableSorter.numericSort = function(a, b) {
<a name="line292"></a>  return parseFloat(a) - parseFloat(b);
<a name="line293"></a>};
<a name="line294"></a>
<a name="line295"></a>
<a name="line296"></a>/**
<a name="line297"></a> * Alphabetic sort function.
<a name="line298"></a> * @param {*} a First sort value.
<a name="line299"></a> * @param {*} b Second sort value.
<a name="line300"></a> * @return {number} Negative if a &lt; b, 0 if a = b, and positive if a &gt; b.
<a name="line301"></a> */
<a name="line302"></a>goog.ui.TableSorter.alphaSort = goog.array.defaultCompare;
<a name="line303"></a>
<a name="line304"></a>
<a name="line305"></a>/**
<a name="line306"></a> * Returns a function that is the given sort function in reverse.
<a name="line307"></a> * @param {function(*, *) : number} sortFunction The original sort function.
<a name="line308"></a> * @return {function(*, *) : number} A new sort function that reverses the
<a name="line309"></a> *     given sort function.
<a name="line310"></a> */
<a name="line311"></a>goog.ui.TableSorter.createReverseSort = function(sortFunction) {
<a name="line312"></a>  return function(a, b) {
<a name="line313"></a>    return -1 * sortFunction(a, b);
<a name="line314"></a>  };
<a name="line315"></a>};
</pre>


</body>
</html>
